import { BadRequestException } from '@nestjs/common';

// 文件上传结果接口
export interface FileUploadResult {
    url: string;
    filename: string;
    mimetype: string;
    size: number;
}

// 文件上传选项接口
export interface FileUploadOptions {
    allowedMimeTypes?: string[];
    maxSize?: number;
    path?: string;
}

// 上传进度接口
export interface UploadProgress {
    loaded: number;
    total: number;
}

// 监控指标接口
export interface FileUploadMetrics {
    uploadCount: number;
    errorCount: number;
    totalUploadSize: number;
    averageUploadTime: number;
}

// 配置接口
export interface FileUploadConfig {
    upload: {
        maxSize: number;
        allowedTypes: string[];
        storageDir: string;
        compressionQuality: number;
    };
    cache: {
        duration: number;
        cleanupInterval: number;
    };
    monitoring: {
        enabled: boolean;
        metricsInterval: number;
    };
}

// 文件服务接口
export interface IFileUploadService {
    uploadFile(file: Express.Multer.File, subDir?: string): Promise<string>;
    uploadMultipleFiles(files: Express.Multer.File[], subDir?: string): Promise<string[]>;
    downloadAndSaveImage(imageUrl: string): Promise<string>;
    uploadFileWithProgress(
        file: Express.Multer.File,
        onProgress?: (progress: UploadProgress) => void
    ): Promise<string>;
    cleanupOldFiles(maxAge: number): Promise<void>;
    compressImage(filePath: string): Promise<void>;
    validateFileContent(file: Buffer): Promise<boolean>;
    deleteFile(filePath: string): Promise<void>;
}

// 错误类型
export class FileUploadError extends Error {
    constructor(
        public readonly code: string,
        message: string,
        public readonly details?: any
    ) {
        super(message);
    }
}

// 常量
export const ALLOWED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
export const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB